--#SET TERMINATOR @
DROP TRIGGER NauczycielDelete@

CREATE TRIGGER NauczycielDelete 
  BEFORE DELETE ON Nauczyciele
  --LANGUAGE SQL
  REFERENCING OLD AS o
  FOR EACH ROW MODE DB2SQL
  BEGIN ATOMIC
    IF (SELECT COUNT(*) FROM KLASY WHERE WYCHOWAWCA = o.id) > 0 THEN
      SIGNAL SQLSTATE '70000';
    END IF;
-- 
    	DELETE FROM UzytkownicySystemu WHERE Nauczyciele_id = o.id;
    	DELETE FROM NauczanePrzedmioty WHERE Nauczyciele_id = o.id;
-- 
  END
  @
  
DROP TRIGGER NauczycielDeleteAfter@

CREATE TRIGGER NauczycielDeleteAfter
	AFTER DELETE ON Nauczyciele
	REFERENCING OLD AS o
  	FOR EACH ROW MODE DB2SQL
  	BEGIN ATOMIC
--     	DELETE FROM UzytkownicySystemu WHERE Nauczyciele_id = o.id;
--     	DELETE FROM NauczanePrzedmioty WHERE Nauczyciele_id = o.id;
  	END
  @
--#SET TERMINATOR ;